int cardCompare(String card1, String card2) {
    String suitPriority = "HCDS"; 
    int a = getValue(card1.substring(0, card1.length() - 1));
    int b = getValue(card2.substring(0, card2.length() - 1));
    char c = card1.charAt(card1.length() - 1);
    char d = card2.charAt(card2.length() - 1);
    if (c != d) {
        return suitPriority.indexOf(c) - suitPriority.indexOf(d);
    } else {
        return Integer.compare(a, b);
    }
}
ArrayList<String> bubbleSort(ArrayList<String> array) {
    for (int i = 0; i < array.size() - 1; i++) {
        for (int j = 0; j < array.size() - i - 1; j++) {
            if (cardCompare(array.get(j), array.get(j + 1)) > 0) {
                String temp = array.get(j);
                array.set(j, array.get(j + 1));
                array.set(j + 1, temp);
            }
        }
    }
    return array;
}
ArrayList<String> mergeSort(ArrayList<String> array) {
    if (array.size() <= 1) {
        return array;
    }
    int middle = array.size() / 2;
    ArrayList<String> left = new ArrayList<>(array.subList(0, middle));
    ArrayList<String> right = new ArrayList<>(array.subList(middle, array.size()));
    return merge(mergeSort(left), mergeSort(right));
}
ArrayList<String> merge(ArrayList<String> left, ArrayList<String> right) {
    ArrayList<String> result = new ArrayList<>();
    int i = 0, j = 0;
    while (i < left.size() && j < right.size()) {
        if (cardCompare(left.get(i), right.get(j)) <= 0) {
            result.add(left.get(i++));
        } else {
            result.add(right.get(j++));
        }
    }
    result.addAll(left.subList(i, left.size()));
    result.addAll(right.subList(j, right.size()));
    return result;
}
long measureBubbleSort(String filename) throws IOException {
    List<String> lines = Files.readAllLines(Paths.get(filename));
    ArrayList<String> cards = lines.stream()
            .filter(line -> line.matches("^(A|[2-9]|10|J|Q|K)(H|C|D|S)$"))
            .collect(Collectors.toCollection(ArrayList::new));
    long start = System.nanoTime();
    bubbleSort(cards);
    long end = System.nanoTime();
    return (end - start) / 1_000_000;
}
long measureMergeSort(String filename) throws IOException {
    List<String> lines = Files.readAllLines(Paths.get(filename));
    ArrayList<String> cards = lines.stream()
            .filter(line -> line.matches("^(A|[2-9]|10|J|Q|K)(H|C|D|S)$"))
            .collect(Collectors.toCollection(ArrayList::new));
    long start = System.nanoTime();
    mergeSort(cards);
    long end = System.nanoTime();
    return (end - start) / 1_000_000;
}
void sortComparison(String[] filenames) throws IOException {
    try (FileWriter writer = new FileWriter("sortComparison.csv")) {
        writer.write("File,Bubble Sort Time (ms),Merge Sort Time (ms)\n");
        for (String filename : filenames) {
            long bubbleSortTime = measureBubbleSort(filename);
            long mergeSortTime = measureMergeSort(filename);
            writer.write(String.format("%s,%d,%d\n", filename, bubbleSortTime, mergeSortTime));
        }
    } catch (IOException e) {
        System.err.println("Error writing to CSV file");
        e.printStackTrace();
        throw e;
    }
}
static int getValue(String value) {
    switch (value) {
        case "A": return 1;
        case "2": return 2;
        case "3": return 3;
        case "4": return 4;
        case "5": return 5;
        case "6": return 6;
        case "7": return 7;
        case "8": return 8;
        case "9": return 9;
        case "10": return 10;
        case "J": return 11;
        case "Q": return 12;
        case "K": return 13;
        default:
            throw new IllegalArgumentException("Invalid card value: " + value);
    }
}
